//
//  Common.m
//  DingDing
//
//  Created by LHL on 14/11/22.
//  Copyright (c) 2014年 com.gordon. All rights reserved.
//

#import "Common.h"
#import "AFNetworking.h"
#import <SDWebImage/SDWebImageDownloader.h>

@implementation Common
+(void)saveUserid:(USERMODEL *)model{
    [[NSUserDefaults standardUserDefaults]setObject:model.user_id forKey:@"user_id"];
    [[NSUserDefaults standardUserDefaults]setObject:model.token forKey:@"token"];
    [[NSUserDefaults standardUserDefaults]setObject:model.im_password forKey:@"im_password"];
    [[NSUserDefaults standardUserDefaults]setObject:model.im_username forKey:@"im_username"];
    [[NSUserDefaults standardUserDefaults]setObject:model.email forKey:@"email"];
    [[NSUserDefaults standardUserDefaults]setObject:model.nickname forKey:@"nickname"];
    if (model.pet_info.count > 0) {
        [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"isaddpet"];
        [Common savePetModel:model.pet_info[0]];
    }else{
        [[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"isaddpet"];
    }
    [[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"logout"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}

+(void)savePetModel:(PETINFOMODEL *)model{
    NSUserDefaults *users = [NSUserDefaults standardUserDefaults];
    [users setObject:model.pet_id forKey:@"pet_id"];
    [users setObject:model.user_id forKey:@"user_id"];
    [users setObject:model.petname forKey:@"petname"];
    [users setObject:model.gender forKey:@"gender"];
    [users setObject:model.signature forKey:@"signature"];
    [users setObject:model.address forKey:@"address"];
    [users setObject:model.variety forKey:@"variety"];
    [users setObject:model.birthday forKey:@"birthday"];
    if (![model.imgurl isEqualToString:[users objectForKey:@"imgurl"]]) {
        [users setObject:model.imgurl forKey:@"imgurl"];
        SDWebImageManager *manager = [SDWebImageManager sharedManager];
        [manager downloadWithURL:[NSURL URLWithString:model.imgurl] options:SDWebImageContinueInBackground progress:^(NSInteger receivedSize, NSInteger expectedSize){
            
        }completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished){
            [[NSUserDefaults standardUserDefaults]setObject:UIImageJPEGRepresentation(image, 0.5) forKey:@"head_img"];
            [[NSUserDefaults standardUserDefaults]synchronize];
        }];
    }
    [users setObject:model.pic_id forKey:@"pic_id"];
    [users synchronize];
    
}
+(void)saveUserInfo:(NSString *)user pwd:(NSString *)pwd{
    NSUserDefaults *users = [NSUserDefaults standardUserDefaults];
    [users setObject:user forKey:@"user"];
    [users setObject:pwd forKey:@"password"];
    [users synchronize];
}

+(void)cleanUserInfo{
     NSUserDefaults *users = [NSUserDefaults standardUserDefaults];
    [users setObject:@"" forKey:@"user_id"];
    [users setObject:@"" forKey:@"token"];
    [users setObject:@"" forKey:@"im_password"];
    [users setObject:@"" forKey:@"im_username"];
    [users setObject:@"" forKey:@"email"];
    [users setObject:@"" forKey:@"nickname"];
    [users setObject:@"" forKey:@"user"];
    [users setObject:@"" forKey:@"password"];
    [users setObject:@"0" forKey:@"isaddpet"];
    [users setObject:@"" forKey:@"petname"];
    [users setObject:@"" forKey:@"address"];
    [users setObject:@"" forKey:@"headimg"];
    [users setObject:@"" forKey:@"imgurl"];
    [users setObject:@"" forKey:@"head_img"];
    [users setObject:@"1" forKey:@"logout"];
    [users synchronize];
}

+(void)saveThirdUserInfo:(THIRDLOGINUSERMODEL *)model{
    NSUserDefaults *users = [NSUserDefaults standardUserDefaults];
    [users setObject:model.user_id forKey:@"user_id"];
    [users setObject:model.token forKey:@"token"];
    [users setObject:model.im_username forKey:@"im_username"];
    [users setObject:model.im_password forKey:@"im_password"];
    [users setObject:model.nickname forKey:@"nickname"];
    if (model.petInfo.count > 0) {
        LOGINPETINFOMODEL *pet = model.petInfo[0];
        [users setObject:@"1" forKey:@"isaddpet"];
        [users setObject:pet.petname forKey:@"petname"];
        [users setObject:pet.address forKey:@"address"];
        [users setObject:pet.imgurl forKey:@"headimg"];
        [users setObject:pet.pet_id forKey:@"pet_id"];
        [users setObject:@"weibo" forKey:@"logintype"];
        SDWebImageManager *manager = [SDWebImageManager sharedManager];
        [manager downloadWithURL:[NSURL URLWithString:pet.imgurl] options:SDWebImageCacheMemoryOnly progress:^(NSInteger receivedSize,NSInteger expectedSize){
            
        }completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished){
        }];
    }else{
        [users setObject:@"0" forKey:@"isaddpet"];
    }
    [users synchronize];
}

+(CGFloat)getItemHeight:(SHAREITEMMODEL *)model{
    CGFloat total = 0;
    NSInteger linecount = 0;
    if (model.image.count >1) {
        if (model.image.count >3) {
            if (model.image.count %3 == 0) {
                linecount = model.image.count/3 ;
            }else{
                linecount = model.image.count/3 +1;
            }
        }else{
            linecount = 1;
        }
        total += linecount*80+10;
    }else{
        if (model.image.count > 0) {
            total += [UIScreen mainScreen].bounds.size.width*0.6875+10;
        }
    }
    NSDictionary *subFont = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
    CGSize subSize;
    if (IOS7_OR_LATER) {
        subSize = [model.summary boundingRectWithSize:CGSizeMake(220, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:subFont context:nil].size;
    }else{
        subSize = [model.summary sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(220, 40) lineBreakMode:NSLineBreakByTruncatingTail];
    }
//    NSLog(@"%f",subSize.height);
    total += subSize.height;
    if (subSize.height <1) {
//        total += 12+10+15;
        total += 12;
    }else{
//        total += 12+10+19+10;
        total += 12 +4;
    }
    
    return total;
}

+ (UIImage *)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
//    NSLog(@"%f-----%f",newImage.size.width,newImage.size.height);
    return newImage;
}

@end

@implementation petsModel
-(id)init{
    if (self =[super init]) {
        self.data = [[NSMutableArray alloc]init];
    }
    return self;
}

@end

@implementation CustomDatePiker{
    UIDatePicker *piker;
    UIView *alphaView;
    UIButton *confirm;
}
-(id)init{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        
        alphaView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        alphaView.backgroundColor = [UIColor blackColor];
        alphaView.alpha = 0;
        
        piker = [[UIDatePicker alloc]initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width-20, 216)];
        piker.datePickerMode = UIDatePickerModeDate;
        piker.backgroundColor = [UIColor whiteColor];
        NSDateComponents *comp = [[NSDateComponents alloc]init];
        [comp setMonth:1];
        [comp setDay:2];
        [comp setYear:1900];
        NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        NSDate *date1 = [calendar dateFromComponents:comp];
        //    NSLog(@" date1--%@",date1);
        NSDate *now = [NSDate date];
        NSTimeZone *GMT = [NSTimeZone systemTimeZone];
        [NSTimeZone setDefaultTimeZone:GMT];
        NSInteger interval = [GMT secondsFromGMTForDate:now];
        NSDate *localdate = [now dateByAddingTimeInterval:interval];
        NSLog(@"local ---%@",localdate);
//        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//        [formatter setDateFormat:@"yyyy-MM-dd"];
        piker.minimumDate = date1;
        piker.maximumDate = localdate;
        confirm = [[UIButton alloc]initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width-20, 45)];
        [confirm setTitle:@"确定" forState:0];
        [confirm setTitleColor:[UIColor whiteColor] forState:0];
        [confirm setBackgroundColor:RGB(247, 90, 83)];
        [confirm addTarget:self action:@selector(sendDate) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    return self;
}

-(void)sendDate{
     NSDate *date = piker.date;
    NSDate *now = [NSDate date];
    NSTimeZone *GMT = [NSTimeZone systemTimeZone];
    [NSTimeZone setDefaultTimeZone:GMT];
    NSInteger interval = [GMT secondsFromGMTForDate:now];
    NSDate *localdate = [now dateByAddingTimeInterval:interval];
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    unsigned int unitFlag;
    if (IOS8_OR_LATER) {
        unitFlag = NSCalendarUnitMonth;
    }else{
        unitFlag  = NSDayCalendarUnit;
    }
    NSDateComponents *day = [cal components:unitFlag fromDate:date toDate:localdate options:0];
    NSInteger distanceMonth = [day month];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *pikerDate = [formatter stringFromDate:date];
    CGRect frame = piker.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height;
    CGRect butframe = confirm.frame;
    butframe.origin.y = [UIScreen mainScreen].bounds.size.height;
    [UIView animateWithDuration:0.25 animations:^{
        piker.frame = frame;
        alphaView.alpha=0.0;
        confirm.frame = butframe;
    }completion:^(BOOL finished){
        if (self.age) {
            self.age(pikerDate,distanceMonth);
        }
        [self removeFromSuperview];
    }];
}

-(void)showPiker{
    [self addSubview:alphaView];
    [self addSubview:piker];
    [self addSubview:confirm];
//    self.backgroundColor = [UIColor blackColor];
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = piker.frame;
        alphaView.alpha=0.1;
        frame.origin.y = [UIScreen mainScreen].bounds.size.height-400;
        piker.frame = frame;
        CGRect butframe = confirm.frame;
        butframe.origin.y = [UIScreen mainScreen].bounds.size.height-163;
        confirm.frame=butframe;
    }];
}

-(void)getAge:(petAge)block{
    self.age = [block copy];
}

@end
